#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e5+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],n;
bool check(LL j,LL i,LL k){

    if(j<1||k>n||i<1||i>n) return false;
    if(a[j]<a[i]&&a[k]<a[i]) return true;
    if(a[j]>a[i]&&a[k]>a[i]) return true;
    return false;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    cin>>n;
    for(LL i=0;i<n+10;i++) a[i]=0;
    for(LL i=1;i<=n;i++) cin>>a[i];
    LL sum=0;
    for(LL i=1;i<=n;i++){
        if( check(i-1,i,i+1)==true ) sum++;
    }
    LL ans=0;
    for(LL i=1;i<=n;i++){
        LL pre=0;
        if(check(i-2,i-1,i)==1) pre++;
        if(check(i-1,i,i+1)==1) pre++;
        if(check(i,i+1,i+2)==1) pre++;
        LL temp=a[i];
        a[i]=a[i-1];
        LL left=0;
        if(check(i-2,i-1,i)==1) left++;
        if(check(i-1,i,i+1)==1) left++;
        if(check(i,i+1,i+2)==1) left++;
        LL right=0;
        a[i]=a[i+1];
        if(check(i-2,i-1,i)==1) right++;
        if(check(i-1,i,i+1)==1) right++;
        if(check(i,i+1,i+2)==1) right++;
        a[i]=temp;
        ans=max(ans,max(pre-left,pre-right));
    }
    cout<<min(sum-ans,sum)<<"\n";
  }
return 0;
}
